package com.service.abolishing.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.abolishing.dto.NceUploadData;
import com.service.abolishing.dto.ValidatorData;
import com.service.abolishing.entity.CloudCampusInfo;
import com.service.abolishing.service.CloudCampusInfoService;
import com.service.abolishing.service.JsonDataService;
import com.service.abolishing.task.ScheduledService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 上报数据表(JsonData)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:35
 */
@RestController
@RequestMapping("getData")
@Api(value = "获取数据接口", tags = "获取数据接口")
public class JsonDataController {
    @Resource
    private CloudCampusInfoService cloudCampusInfoService;
    /**
     * 服务对象
     */
    @Resource
    private JsonDataService jsonDataService;
@Resource
    ScheduledService scheduledService;
    /**
     * 校验
     * @param validator
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/lbs/{validator}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "校验")
    public String getRequest(@PathVariable("validator") @ApiParam("校验参数") String validator) throws JsonProcessingException {
        //在云平台请求时，返回数据格式为 {"validator":xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
        List<CloudCampusInfo> cloudCampusInfoList = cloudCampusInfoService.queryByValidator(validator);
        if (cloudCampusInfoList.size() > 0) {
            ValidatorData validatorData = new ValidatorData(validator);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(validatorData);
        }
        ValidatorData validatorData = new ValidatorData("null");
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(validatorData);
    }

    /**
     * 接收数据
     * @param body
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/lbs", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "接收数据")
    public String postRequest(@RequestBody String body) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        NceUploadData lbsEntity = mapper.readValue(body, NceUploadData.class);
        String secret = lbsEntity.getSecret();
        CloudCampusInfo cloudCampusInfo = cloudCampusInfoService.queryBySecret(secret);
        //校验secret与预设是否相符
        if (cloudCampusInfo == null) {
            return "error";
        }
        scheduledService.getLoginUserDispose12();
        //jsonDataService.insertList(lbsEntity,cloudCampusInfo);
        return body;
    }

}
